package com.xique.park.service.biz.service.impl;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Base64Util;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.service.biz.service.IvlprBizService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class VlprBizServiceImpl implements IvlprBizService {

    //设置APPID/AK/SK
    //@Value("${BAIDU.APPID}")
    private String APP_ID;
    //@Value("${BAIDU.APIKEY}")
    private String API_KEY;
    //@Value("${BAIDU.SECRETKEY}")
    private String SECRET_KEY;

    @Override
    public AjaxResult baiduVlpr(String base64Img){

        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("multi_detect", "false");
        // 参数为本地图片路径
//        String image = "C:\\Users\\liuxueli\\Desktop\\1.png";
//        JSONObject res = client.plateLicense(image, options);
        byte[] byteArr = Base64Util.decode(base64Img);
        JSONObject res = client.plateLicense(byteArr, options);
        Map<String,Object> map = new HashMap<>();
        if(!res.isNull("error_code")){
            map.put("containPlate","N");
            Map<String,Object> errInfo  = new HashMap<>();
            errInfo.put("message",res.get("error_msg"));
            errInfo.put("code",res.get("error_code"));
            map.put("errInfo",errInfo);
            return AjaxResult.error("车牌识别失败", map);
        }
        JSONObject wordsResult = res.getJSONObject("words_result");
        if(null != wordsResult){
            Long logId = res.getLong("log_id");
            String number = wordsResult.getString("number");
            String color = wordsResult.getString("color");
            map.put("containPlate","Y");
            Map<String,Object> plateInfo  = new HashMap<>();
            plateInfo.put("plate",number);
            plateInfo.put("color",color);
            map.put("plateInfo",plateInfo);
        }else{
            map.put("containPlate","N");
        }
        //TODO 是否保存到oss?
        return AjaxResult.success(map);
    }



        /**
         * 重要提示代码中所需工具类
         * FileUtil,Base64Util,HttpUtil,GsonUtils请从
         * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
         * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
         * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
         * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
         * 下载
         */
   /*     public String licensePlate(String base64Img) {
            // 请求url
            String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
            try {
                // 本地文件路径
                String filePath = "[本地文件路径]";
                byte[] imgData = FileUtil.readFileByBytes(filePath);
                String imgStr = Base64Util.encode(imgData);
                String imgParam = URLEncoder.encode(base64Img, "UTF-8");

                String param = "image=" + imgParam;

                // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
                String accessToken = "[调用鉴权接口获取的token]";

                String result = HttpUtil.post(url, accessToken, param);
                System.out.println(result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }*/


}
